﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data.SqlClient;
using System.Configuration;

public partial class Android_Login : System.Web.UI.Page
{
    protected class USchedule
    {
        public string CalorieAbsorbed { get; set; }
        public string TimeExercise { get; set; }
        public string Weight { get; set; }
        public string WaterAbsorbed { get; set; }
        public string Date { get; set; }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (Membership.ValidateUser(Request.QueryString["username"], Request.QueryString["pw"]))
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
            SqlCommand cmd;
            SqlDataReader reader;
            List<USchedule> lUSchedule= new List<USchedule>();


            try
            {
                conn.Open();

                cmd = new SqlCommand("SELECT * FROM [Schedule] WHERE [UserId]=@UserId ORDER BY [date] DESC", conn);
                cmd.Parameters.AddWithValue("@UserId", Request.QueryString["username"]);
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    USchedule us = new USchedule()
                    {
                        CalorieAbsorbed = reader["CalorieAbsorbed"].ToString(),
                        TimeExercise = reader["TimeExercise"].ToString(),
                        Weight = reader["Weight"].ToString(),
                        WaterAbsorbed = reader["WaterAbsorbed"].ToString(),
                        Date = Convert.ToDateTime(reader["date"].ToString()).ToString("yyyy-MM-dd")
                    };
                    lUSchedule.Add(us);

                }
                reader.Close();
                string target = "0";
                cmd = new SqlCommand("SELECT TargetWeight FROM UserInformation WHERE UserId=@UserId", conn);
                cmd.Parameters.AddWithValue("@UserId", Request.QueryString["username"]);
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    target = reader["TargetWeight"].ToString();
                }
                reader.Close();
                Dictionary<string, List<USchedule>> user = new Dictionary<string, List<USchedule>>();
                user.Add("Schedule", lUSchedule);
                Dictionary<string, string> Dtarget = new Dictionary<string, string>();
                Dtarget.Add("target", target);
                object[] arrStringA = { Dtarget, user };
                Dictionary<string, object> output = new Dictionary<string, object>();
                output.Add("android", lUSchedule);
                System.Web.Script.Serialization.JavaScriptSerializer objSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                Response.Write(objSerializer.Serialize(output));


            }
            finally
            {
                conn.Close();
            }

        }
        else
            Response.Write("false");


    }
}